/*!
\copyright  Copyright (c) 2024 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file       
\ingroup    hci_tap
\brief      This is the interface to HCI TAP Stream that logs the BT HCI
            Interface traffic out off the chip for debugging purpose.

            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            WARNING: calling hciTapStream_ConfigureUart functions in the code
            by default is a security risk as the HCI Interface provides the
            LINK Keys.

            Therefore it MUST NEVER be called in the code by DEFAULT in a
            final product.
            !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

            Redirection from HCI TAP Stream to UART with pins configuration
            Only available for QCC51xx - QCC30xx variant

            QCC5170_AA_LAB_BRD_LEA workspace
            ================================
            >>>
            uart_rate = 8192 # VM_UART_RATE_2000K
            board_uart_rts = 0x16 # PIO 22
            board_uart_cts = 0x17 # PIO 23
            board_uart_tx = 0x14 # PIO 20
            board_uart_rx = 0x15 # PIO 21
            apps1.fw.call.HciTapStream_ConnectToUart(uart_rate, board_uart_rts,
                                  board_uart_cts, board_uart_tx, board_uart_rx)

            QCC5171_AA_DEV_BRD_R3_AA_LEA
            ============================
            Unfortunatly PIO 22 and PIO 23 are not available for cts rts on
            SODIMM 20-18570-4.
            Because any PIO function can be multiplexed to any PIO in 517x data
            sheet it possible to multiplex functions that are not used for a
            particular application, for instance SPI/I²C, SPDIF, and digital
            microphone.
            CTS and RTS are mapped to pin 16 and pio 17
            >>>
            uart_rate = 8192 # VM_UART_RATE_2000K
            board_uart_rts = 0x10 # PIO 16
            board_uart_cts = 0x11 # PIO 17
            board_uart_tx = 0x14 # PIO 20
            board_uart_rx = 0x15 # PIO 21
            apps1.fw.call.HciTapStream_ConnectToUart(uart_rate, board_uart_rts,
                                  board_uart_cts, board_uart_tx, board_uart_rx)
@{
*/

#include <hci_tap.h>
#include <hci_tap_private.h>
#include <logging.h>

#if defined(ENABLE_HCI_TAP_STREAM)

HciTapPrivate hciTapPrivateData;

#define NB_BITS_IN_MASK ((uint8)32)
static bool hciTapStream_ConfigureUart(vm_uart_rate uart_rate,
    uint8 board_uart_rts,
    uint8 board_uart_cts,
    uint8 board_uart_tx,
    uint8 board_uart_rx)
{
    bool result = FALSE;
    if (board_uart_rts < NB_BITS_IN_MASK &&
        board_uart_cts < NB_BITS_IN_MASK &&
        board_uart_tx < NB_BITS_IN_MASK &&
        board_uart_rx < NB_BITS_IN_MASK)
    {
        /* Assign the appropriate PIOs to be used by UART. */
        uint32 bank = 0;
        uint32 mask = (1<<board_uart_rts) | (1<<board_uart_cts) | (1<<board_uart_tx) | (1<<board_uart_rx);
        DEBUG_LOG_INFO(HCI_TAPI_STR"Pins RTS:%d CTS:%d TX:%d RX:%d" ,
            board_uart_rts, board_uart_cts, board_uart_tx, board_uart_rx);
        PioSetMapPins32Bank(bank, mask, 0);
        PioSetFunction(board_uart_rts, UART_RTS);
        PioSetFunction(board_uart_cts, UART_CTS);
        PioSetFunction(board_uart_tx, UART_TX);
        PioSetFunction(board_uart_rx, UART_RX);
        StreamUartConfigure(uart_rate, VM_UART_STOP_ONE, VM_UART_PARITY_NONE);
        result = TRUE;
    }
    return result;
}

static bool hciTapStream_ConnectToUartComplete(void)
{
    Source src = StreamHciTapSource(); /* should be first */
    Sink sink = StreamUartSink();
    bool retVal = hciTapStream_ConnectStream(src, sink);
    if (retVal)
    {
        DEBUG_LOG_INFO(HCI_TAPI_STR"CONNECTED to Uart Stream");
        hciTapPrivateData.state = HCI_TAP_CONNECTED_TO_UART;
    }
    return retVal;
}

bool HciTapStream_ConnectToUart(vm_uart_rate uart_rate,
                                uint8 board_uart_rts,
                                uint8 board_uart_cts,
                                uint8 board_uart_tx,
                                uint8 board_uart_rx)
{
    if (HCI_TAP_DISCONNECTED != hciTapPrivateData.state)
    {
        DEBUG_LOG_WARN(HCI_TAPW_STR"ALREADY CONNECTED - call testHciTapStream_Disconnect first");
        return FALSE;
    }
    if (hciTapStream_ConfigureUart(uart_rate, board_uart_rts, board_uart_cts,
            board_uart_tx, board_uart_rx))
    {
        return hciTapStream_ConnectToUartComplete();
    }
    return FALSE;
}

bool HciTapStream_ConnectToUartWithSerialEngine(vm_uart_rate uart_rate,
                                                uint8 se_number)
{
    UNUSED(uart_rate);
    UNUSED(se_number);
    DEBUG_LOG_WARN(HCI_TAPW_STR"Function HciTapStream_ConnectToUartWithSerialEngine\
 not available for QCC72xx - QCC52xx");
    return FALSE;
}

#endif